package cn.doitedu.test

import java.io.{ByteArrayInputStream, ByteArrayOutputStream, ObjectInputStream, ObjectOutputStream}

import org.roaringbitmap.RoaringBitmap

object RoaringBitmapDemo {

  def main(args: Array[String]): Unit = {

    // 构造一个roaringbitmap
    val bitmap1 = RoaringBitmap.bitmapOf(1, 5, 6, 9)
    val bitmap2 = RoaringBitmap.bitmapOf(1, 9,11)

    println(bitmap1.getCardinality)
    println(bitmap2.getCardinality)

    bitmap1.or(bitmap2)
    println(bitmap1.getCardinality)

    // 构造一个空的bitmap
    val bm = new RoaringBitmap()
    bm.or(bitmap1)
    println(bm.getCardinality)


    // roaringbitmap的序列化
    val baout = new ByteArrayOutputStream()
    val objOut = new ObjectOutputStream(baout)

    objOut.writeObject(bm)

    // bm被序列化的结果字节数组
    val bmArray = baout.toByteArray


    // 反序列化
    val in = new ObjectInputStream(new ByteArrayInputStream(bmArray))
    val obj = (in.readObject()).asInstanceOf[RoaringBitmap]
    println(obj.getCardinality)


  }

}
